package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.QuotationSet;
import com.sz.biz.logistics.core.service.QuotationSetService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: QuotationSetServiceImpl. <br>
 * Date: 2017年05月08日 17:43 <br>
 * Author: wentao.chang
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class QuotationSetServiceImpl extends AbstractService implements QuotationSetService {
    @Override
    public int save(QuotationSet entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_SET", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_QUOTATION_SET, UserActions.ADD, "添加运输重量段价格设置", dataBackup);
        return entry.getId();
    }

    @Override
    public void batchsave(List<QuotationSet> list) {
        dao.batchInsert(getSqlName("insertSelective"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_QUOTATION_SET", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_QUOTATION_SET, UserActions.ADD, "添加运输重量段价格设置", dataBackup);

    }

    @Override
    public List<QuotationSet> findByQuotationId(Integer quotationId) {
        ParamData pd = new ParamData();
        pd.put("quotationId", quotationId);
        return dao.findForList(getSqlName("findByQuotationId"), pd, QuotationSet.class);
    }

    @Override
    public QuotationSet findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        QuotationSet entry = (QuotationSet) dao.findForObject(getSqlName("selectByPrimaryKey"), pd);
        return entry;
    }

    /**
     * 根据quotationId删除
     *
     * @param quotationId quotationId
     */
    @Override
    public void deletedByQuotationId(Integer quotationId) {
        dao.delete(getSqlName("deletedByQuotationId"), quotationId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_QUOTATION_SET", quotationId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_QUOTATION_SET, UserActions.DELETE, "删除价格设置", dataBackup);
    }

    @Override
    public void update(QuotationSet quotationSet) {
        dao.update(getSqlName("updateById"), quotationSet);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_QUOTATION_SET", quotationSet);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_QUOTATION_SET, UserActions.UPDATE, "修改价格设置", dataBackup);

    }

    @Override
    public void batchDeleteQuotationSet(List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            dao.batchDelete(getSqlName("deleteByIds"), ids);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("DELETE_QUOTATION_SET", ids);
            PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                    ModuleNames.BIZ_QUOTATION_SET, UserActions.DELETE, "删除价格设置", dataBackup);
        }
    }

    @Override
    protected String getMapperNamespace() {
        return "QuotationSetMapper";
    }
}
